
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 6 (IND) ****************************
********************** THIS DO FILE RUNS THE LIKELIHOOD ************************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/model_estimation"'
cap mkdir `"$output/model_estimation/relax_IND"'
cap mkdir `"$output/log/relax_IND_6_model_estimation"'
cap mkdir `"$output/model_estimation/relax_IND/parameters"'
	
cap log close 
log using `"$output/log/relax_IND_6_model_estimation/relax_IND_6_model_estimation.smcl"', replace 

di "This run uses code version from 2023/03/29"



****************************************************************
****************************************************************
******************* Model Estimation by Size *******************
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear


//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 


gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen claim_cost = .
gen claim_cost_se = .

gen claim_cost_adm = . 
gen claim_cost_adm_se = . 
gen claim_cost_cont = . 
gen claim_cost_cont_se = . 
gen claim_cost_cov = . 
gen claim_cost_cov_se = . 
gen claim_cost_dup = . 
gen claim_cost_dup_se = . 
gen claim_cost_info = . 
gen claim_cost_info_se = . 

label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

label var claim_cost_adm "Claim Parameter Administrative"
label var claim_cost_adm_se "Claim Parameter Administrative SE"

label var claim_cost_cont "Claim Parameter Contractual Adjustment"
label var claim_cost_cont_se "Claim Parameter Contractual Adjustment SE"

label var claim_cost_cov "Claim Parameter Coverage"
label var claim_cost_cov_se "Claim Parameter Coverage SE"

label var claim_cost_dup "Claim Parameter Duplicate"
label var claim_cost_dup_se "Claim Parameter Duplicate SE"

label var claim_cost_info "Claim Parameter Information"
label var claim_cost_info_se "Claim Parameter Information SE"


cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	

forvalues size = 1/2 {
	
	if `size' == 1 {
		local grup_size "small"
	}
	
	if `size' == 2 {
		local grup_size "large"
	}
	

	forvalues x = 1/3 {
		
		if `x' == 1 {
			local payer "Medicare"
		}
		
		else if `x' == 2 {
			local payer "Medicaid"
		}
		
		else if `x' == 3 {
			local payer "Commercial"
		}
		
		forvalues r = 1/5 {
		
			if `r' == 1 {
				local reason "administrative"
			}
			
			else if `r' == 2 {
				local reason "contractual"
			}
			
			else if `r' == 3 {
				local reason "coverage"
			}
		
			else if `r' == 4 {
				local reason "duplicate"
			}
			
			
			else if `r' == 5 {
				local reason "information"
			}
		
		
			** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
			di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
			clogit choice_S _Istate* continuation if pay_type_pooled ==`x' & visit_code_cat_n == `r' & size_indicator == `size' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
			
			est save `"$output/model_estimation/relax_IND/parameters/model_est_`payer'_`reason'_`grup_size'groups.ster"', replace 

			
			forv s = 1/54 {
				cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
				cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
			}


		
		
			if (`r' == 1) {
			
				forv s = 1/54 {
				
					cap replace claim_cost_adm = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_adm_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 2) {
			
				forv s = 1/54 {
				
					cap replace claim_cost_cont = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cont_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					
				}
			}
			
			else if (`r' == 3) {
			
				forv s = 1/54 {
				
					cap replace claim_cost_cov = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cov_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 4) {
			
				forv s = 1/54 {
				
					cap replace claim_cost_dup = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_dup_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 5) {
				
				forv s = 1/54 {
				
					cap replace claim_cost_info = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_info_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
		}		
	
	}

}

* keep relevant observations
bys pay_type_pooled state visit_code_cat size_indicator: keep if _n == 1
keep pay_type_pooled state claim_cost* payee_state_cd visit_code_cat visit_code_cat_n size_indicator
compress

save "$output/model_estimation/relax_IND/estimations_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state size_indicator: keep if _n == 1
cap drop claim_cost claim_cost_se visit_code_cat visit_code_cat_n size_indicator

save "$output/model_estimation/relax_IND/estimations_medicaid_bin.dta", replace 







****************************************************************
****************************************************************
********** Model Estimation No Size Distinction ****************
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear

//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 


gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen claim_cost = .
gen claim_cost_se = .

gen claim_cost_adm = . 
gen claim_cost_adm_se = . 
gen claim_cost_cont = . 
gen claim_cost_cont_se = . 
gen claim_cost_cov = . 
gen claim_cost_cov_se = . 
gen claim_cost_dup = . 
gen claim_cost_dup_se = . 
gen claim_cost_info = . 
gen claim_cost_info_se = . 

label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

label var claim_cost_adm "Claim Parameter Administrative"
label var claim_cost_adm_se "Claim Parameter Administrative SE"

label var claim_cost_cont "Claim Parameter Contractual Adjustment"
label var claim_cost_cont_se "Claim Parameter Contractual Adjustment SE"

label var claim_cost_cov "Claim Parameter Coverage"
label var claim_cost_cov_se "Claim Parameter Coverage SE"

label var claim_cost_dup "Claim Parameter Duplicate"
label var claim_cost_dup_se "Claim Parameter Duplicate SE"

label var claim_cost_info "Claim Parameter Information"
label var claim_cost_info_se "Claim Parameter Information SE"


cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	

forvalues x = 1/3 {
	
	if `x' == 1 {
		local payer "Medicare"
	}
	
	else if `x' == 2 {
		local payer "Medicaid"
	}
	
	else if `x' == 3 {
		local payer "Commercial"
	}
	
	forvalues r = 1/5 {
	
		if `r' == 1 {
			local reason "Administrative"
		}
		
		else if `r' == 2 {
			local reason "contractual"
		}
		
		else if `r' == 3 {
			local reason "coverage"
		}
	
		else if `r' == 4 {
			local reason "duplicate"
		}
		
		
		else if `r' == 5 {
			local reason "information"
		}
	
	
		** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
		di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
		clogit choice_S _Istate* continuation if pay_type_pooled ==`x' & visit_code_cat_n == `r' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
		
		est save `"$output/model_estimation/relax_IND/parameters/model_est_`payer'_`reason'_allgroups.ster"', replace 
		
		forv s = 1/54 {
			cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' &    visit_code_cat_n == `r'
			cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x' &    visit_code_cat_n == `r'
		}


	
	
		if (`r' == 1) {

			forv s = 1/54 {
			
				cap replace claim_cost_adm = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_adm_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 2) {

			forv s = 1/54 {
			
				cap replace claim_cost_cont = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_cont_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				
			}
		}
		
		else if (`r' == 3) {

			forv s = 1/54 {
			
				cap replace claim_cost_cov = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_cov_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 4) {

			forv s = 1/54 {
			
				cap replace claim_cost_dup = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_dup_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
		else if (`r' == 5) {
			
			forv s = 1/54 {
			
				cap replace claim_cost_info = _b[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
				cap replace claim_cost_info_se = _se[_Istate_`s'] if pay_type_pooled == `x' & state == `s'
			
			}
		}
		
	}		

}



* keep relevant observations
bys pay_type_pooled state visit_code_cat: keep if _n == 1
keep pay_type_pooled state claim_cost* payee_state_cd visit_code_cat visit_code_cat_n
compress

save "$output/model_estimation/relax_IND/estimations_nosize_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state: keep if _n == 1
cap drop claim_cost claim_cost_se visit_code_cat visit_code_cat_n

save "$output/model_estimation/relax_IND/estimations_nosize_medicaid_bin.dta", replace 






****************************************************************
****************************************************************
******* Model Estimation No Size and Reason Distinction ********
****************************************************************
****************************************************************
use  `"$output/temp/relax_IND_estimation_data_bin.dta"', clear

//set criteria for observations to be included in the liklihood
bys pay_type_pooled state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_pooled state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 

** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen claim_cost = .
gen claim_cost_se = .

label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	

forvalues x = 1/3 {
	
	if `x' == 1 {
		local payer "Medicare"
	}
	
	else if `x' == 2 {
		local payer "Medicaid"
	}
	
	else if `x' == 3 {
		local payer "Commercial"
	}
	
	
	** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
	di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
	clogit choice_S _Istate* continuation if pay_type_pooled ==`x' & valid_cell == 1, constraint(1) group(visit_billing_id) iter($iterations)
	
	est save `"$output/model_estimation/relax_IND/parameters/model_est_`payer'_allreasons_allgroups.ster"', replace 

	
	forv s = 1/54 {
		cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_pooled == `x' 
		cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_pooled == `x'
	}	
	
}		



* keep relevant observations
bys pay_type_pooled state: keep if _n == 1
keep pay_type_pooled state claim_cost* payee_state_cd
compress

save "$output/model_estimation/relax_IND/estimations_nosize_noreason_bin.dta", replace 

keep if pay_type_pooled == 2 
bys pay_type_pooled state: keep if _n == 1
cap drop claim_cost claim_cost_se visit_code_cat visit_code_cat_n

save "$output/model_estimation/relax_IND/estimations_nosize_noreason_medicaid_bin.dta", replace 


log close


